const FILE_SCHEME = "file://"
const INLINE_IMG_WIDTH = 200
const INLINE_IMG_HEIGHT = 200

import { RandomUtil } from '@ohos/base/src/main/ets/utils/RandomUtil'
import type { InlineAttachment } from '@ohos/mail-app/src/main/ets/app'

export class RichEditorUtil {
  static replaceContent(content: string): string {
    content = content.substring(1, content.length - 1); // 去掉前后引号
    content = content.replace(/\\u003C/g, "<");
    content = content.replace(/\\\"/g, '"');
    content = content.replace(/\\n/g, '\n');
    return content;
  }

  static replaceImg(content: string, inlineAttachments: Array<InlineAttachment>): string {
    content = content.replace(/<img.*?src=\"(.*?)\"[^>]*>/g, (s, filename: string) => {
      // content = content.replace(/<img.*?src=\"(.*?)\">/g, function(s, filename: string) {
      let cid: string = RandomUtil.generateRandomNumberStr(13)
      let imgInsert: string = "img_insert_" + cid;
      let inlineImage: InlineAttachment = { contentId: cid,
        file: filename.substring(FILE_SCHEME.length, filename.length) };

      inlineAttachments.push(inlineImage);
      return `<img src="cid:${cid}"  id="${imgInsert}" width=${INLINE_IMG_WIDTH}  height=${INLINE_IMG_HEIGHT} /> `
    })
    return content;
  }
}